package io.renren.service.impl;

import com.alibaba.fastjson.JSONObject;
import io.renren.dao.SbydglDao;
import io.renren.entity.SbydglEntity;
import io.renren.entity.SysUserEntity;
import io.renren.utils.DateUtils;
import io.renren.utils.RRException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.dao.SbydglDetDao;
import io.renren.entity.SbydglDetEntity;
import io.renren.service.SbydglDetService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Transactional(propagation= Propagation.REQUIRED)
@Service("sbydglDetService")
public class SbydglDetServiceImpl implements SbydglDetService {
	/**
	 * 日志对象
	 */
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private SbydglDetDao sbydglDetDao;
	@Autowired
	private SbydglDao sbydglDao;
	
	@Override
	public SbydglDetEntity queryObject(Integer yddetId){
		return sbydglDetDao.queryObject(yddetId);
	}
	
	@Override
	public List<SbydglDetEntity> queryList(Map<String, Object> map){
		return sbydglDetDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return sbydglDetDao.queryTotal(map);
	}
	
	@Override
	public void save(SbydglDetEntity sbydglDet,SysUserEntity sysUserEntity){
		sbydglDet.setInputperson(sysUserEntity.getUsername());
		sbydglDet.setEndDate(new Date());
		sbydglDetDao.save(sbydglDet);
	}

	@Override
	public void simpleAudit(JSONObject params, SysUserEntity sysUserEntity) {
		int count=0;
		try {
			SbydglDetEntity sbydglDetEntity = new SbydglDetEntity();
			sbydglDetEntity.setYddetId(Integer.parseInt(params.get("id").toString()));
			if("0".equals(params.get("audit").toString())){
				count=1;
			}else if("1".equals(params.get("audit").toString())){
				sbydglDetEntity.setInstastatus("0");
				count = sbydglDetDao.queryCount(sbydglDetEntity);
			}else if("2".equals(params.get("audit").toString())){
				sbydglDetEntity.setInstastatus("1");
				count =sbydglDetDao.queryCount(sbydglDetEntity);
			}else if("3".equals(params.get("audit").toString())){
				sbydglDetEntity.setInstastatus("2");
				count =sbydglDetDao.queryCount(sbydglDetEntity);
			}
			if(count!=1){
				throw new RRException("数据可能已提交，请刷新页面！");
			}
			sbydglDetEntity.setInstastatus(params.get("audit").toString());
			sbydglDetEntity.setFirstinstaperson(sysUserEntity.getUsername());
			sbydglDetEntity.setFirstinstance(new Date());
			sbydglDetDao.simpleAudit(sbydglDetEntity);
		}catch (RRException e) {
			logger.info("e",e);
			throw e;
		}

	}

	@Override
	public void replace(JSONObject params, SysUserEntity sysUserEntity) {
		SbydglDetEntity sbydglDetEntityreplace = new SbydglDetEntity();
		SbydglDetEntity sbydglDetEntityreplaced = new SbydglDetEntity();
		sbydglDetEntityreplace = sbydglDetDao.queryObject(params.getInteger("replace_id"));//替换人
		sbydglDetEntityreplaced = sbydglDetDao.queryObject(params.getInteger("replaced_id"));//被替换人
		//设置替换人信息
		sbydglDetEntityreplace.setStartDate(new Date());
		sbydglDetEntityreplace.setEndDate(sbydglDetEntityreplaced.getEndDate());
		sbydglDetEntityreplace.setFirstinstance(new Date());
		sbydglDetEntityreplace.setFirstinstaperson(sysUserEntity.getUsername());
		sbydglDetEntityreplace.setSecondinstance(new Date());
		sbydglDetEntityreplace.setSecondinstperson(sysUserEntity.getUsername());
		sbydglDetEntityreplace.setThreeinstance(new Date());
		sbydglDetEntityreplace.setThreeinsteperson(sysUserEntity.getUsername());
		sbydglDetEntityreplace.setInstastatus("3");
		sbydglDetEntityreplace.setEnabled("1");
		//设置被替换人信息
		sbydglDetEntityreplaced.setEndDate(new Date());
		sbydglDetEntityreplaced.setReplacePerson(sbydglDetEntityreplaced.getUserName());
		sbydglDetEntityreplaced.setEnabled("2");
		sbydglDetDao.update(sbydglDetEntityreplace);
		sbydglDetDao.update(sbydglDetEntityreplaced);
	}


	@Override
	public void update(SbydglDetEntity sbydglDet){
		try {
		/*	SbydglEntity sbydglEntity = sbydglDao.queryObject(sbydglDet.getYdId());
			if (sbydglDet.getStartDate() != null && sbydglDet.getEndDate() != null) {
				int months = DateUtils.joinMonthsContain(sbydglDet.getStartDate(), sbydglDet.getEndDate());
				double moeny = sbydglEntity.getBxje();
				sbydglDet.setSjjfje(SbydglServiceImpl.getSjjfje(months, moeny));//通过生效日期和失效日期来算出对应的实际交付金额
			}*/
			sbydglDetDao.update(sbydglDet);
		}catch (RRException e) {
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void delete(Integer yddetId){
		sbydglDetDao.delete(yddetId);
	}
	
	@Override
	public void deleteBatch(Integer[] yddetIds){
		sbydglDetDao.deleteBatch(yddetIds);
	}
	
}
